# nodemon --watch fetcher/03request_check_ua.py --exec 'uv run fetcher/03request_check_ua.py'
import requests


url = 'http://www.cpta.com.cn/'
# url = 'http://localhost:3000/'

#伪装浏览器请求
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}

resp = requests.get(url, headers=header)
resp.encoding = 'UTF-8' # fix 中文乱码
# print(resp.text)


with open('./tmp/check_ua.html', 'w', encoding='UTF-8') as f:
    f.write(resp.text)


# 检查ua 是不是浏览器，返回：<!doctypehtml><html lang="zh-cn"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible"content="IE=edge,chrome=1"><meta name="data-spm"content="a3c0e"><title>405</title><style>a,body,div,h2,html,p{margin:0;padding:0}a{text-decoration:none;color:#3b6ea3}.container{width:1000px;margin:auto;color:#696969}.header{padding:110px 0}.header .message{height:36px;padding-left:120px;background:url(https://errors.aliyun.com/images/TB1TpamHpXXXXaJXXXXeB7nYVXX-104-162.png) no-repeat 0 -128px;line-height:36px}.main{padding:50px 0;background:#f4f5f7}#block_image{position:relative;left:120px}</style><body data-spm="7663354"><div data-spm="1998410538"><div class="header"><div class="container"><div class="message"><div id="block_message"></div><div><span id="block_url_tips"></span><strong id="url"></strong></div><div><span id="block_time_tips"></span><strong id="time"></strong></div><div><span id="block_traceid_tips"></span><strong id="traceid"></strong></div></div></div></div><div class="main"><div class="container"><img id="block_image"></div></div></div><script>function getRenderData(){var e=document.getElementById("renderData");return JSON.parse(e.innerHTML)}function convertTimestampToString(e){e=parseInt(e,10),e=new Date(e);return e.getFullYear()+"-"+("0"+(e.getMonth()+1)).slice(-2)+"-"+("0"+e.getDate()).slice(-2)+" "+("0"+e.getHours()).slice(-2)+":"+("0"+e.getMinutes()).slice(-2)+":"+("0"+e.getSeconds()).slice(-2)}var en_tips={block_message:"Sorry, your request has been blocked as it may cause potential threats to the server's security.",block_url_tips:"Current URL: ",block_time_tips:"Request Time: ",block_traceid_tips:"Your Request ID is: "},cn_tips={block_message:"很抱歉，由于您访问的URL有可能对网站造成安全威胁，您的访问被阻断。",block_url_tips:"当前网址: ",block_time_tips:"请求时间: ",block_traceid_tips:"您的请求ID是: "};window.onload=function(){var t=getRenderData(),n="cn";try{navigator.language.startsWith("zh")||(n="en")}catch(e){t.lang&&(n=t.lang)}if(t){var e,i=cn_tips,r=document.getElementById("block_image");for(e in"en"===n?(i=en_tips,r.src="https://g.alicdn.com/sd-base/static/1.0.5/image/405.png",r.id="en_block"):r.src="https://errors.aliyun.com/images/TB15QGaHpXXXXXOaXXXXia39XXX-660-117.png",i)document.getElementById(e).innerText=i[e];n=t.traceid,r=n.slice(8,21);document.getElementById("traceid").innerText=n,document.getElementById("url").innerText=location.href.split("?")[0],document.getElementById("time").innerText=convertTimestampToString(r)}}</script><textarea id="renderData" style="display:none">{"traceid":"0a47318317537782738662336e006d","lang":"cn"}</textarea>    

## 默认请求头
# GET / HTTP/1.1
# Host: localhost:3000
# User-Agent: python-requests/2.32.4
# Accept-Encoding: gzip, deflate
# Accept: */*
# Connection: keep-alive